package LeetCode._01算法入门.day08广度深度优先搜索;

/**
 * @author 挚爱之夕
 * @date 2022-02-27 - 02 - 27 - 14:01
 * @Description 如果两个节点重叠，那么将这两个节点的值相加作为合并后节点的新值
 * 否则，不为 null 的节点将直接作为新二叉树的节点。
 * @Version 1.0
 */
public class _617合并二叉树 {
    public static void main(String[] args) {

    }
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null) {
            return t2;
        }
        if (t2 == null) {
            return t1;
        }
        //合并根
        TreeNode merged = new TreeNode(t1.val + t2.val);
        //合并左树
        merged.left = mergeTrees(t1.left, t2.left);
        //合并右树
        merged.right = mergeTrees(t1.right, t2.right);
        return merged;
    }
}
